package java44th.group.controller.admin.issue;

import java44th.group.bean.admin.bo.issue.IssueCreateBo;
import java44th.group.bean.admin.bo.issue.IssueUpdteBo;
import java44th.group.bean.common.BasePageInfo;
import java44th.group.bean.common.CommonData;
import java44th.group.bean.po.MarketIssue;
import java44th.group.bean.admin.vo.BaseRespVo;
import java44th.group.service.admin.issue.IssueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 小焦
 * @since 2022/10/12 17:58
 */

@RestController
@RequestMapping("admin/issue")
public class IssueController {
    @Autowired
    IssueService issueService;

    @GetMapping("list")
    public BaseRespVo list(BasePageInfo info,String question) {
        CommonData<MarketIssue> data= null;
        try {
            data = issueService.list(info,question);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.error(402, "参数值不对");
        }
        return BaseRespVo.ok(data);
    }

    /**
     *  1. 获取请求参数--{"question":"11","answer":"11"}
     * 2. 处理业务逻辑--创建一条issue记录
     * 3. 返回响应--{"id":41,"question":"你礼貌吗","answer":"爱你孤身走暗巷","addTime":"2022-10-12 22:39:46","updateTime":"2022-10-12 22:39:46"}
     * @param issueCreateBo
     * @return java44th.group.bean.admin.vo.BaseRespVo
     * @author 小焦
     * @since 2022/10/12 下午 10:37
     */
    @PostMapping("create")
    public BaseRespVo create(@RequestBody IssueCreateBo issueCreateBo){
        MarketIssue issue= null;
        try {
            issue = issueService.create(issueCreateBo);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.error(402, "参数值不对");
        }
        if(issue==null){
        return BaseRespVo.invalidParameter("添加失败,联系管理员");
    }
        return BaseRespVo.ok(issue);
    }

    /**
     * 1. 获取请求参数--{"id":16,"question":"雷克萨大姐夫","answer":"啥地方沙发","addTime":"2022-10-12 23:17:57","updateTime":"2022-10-12 23:17:57","deleted":null}
     * 2. 处理业务逻辑--更新回复记录
     * 3. 返回响应--{"id":41,"question":"你礼貌吗","answer":"爱你孤身走暗巷,趣里的撒","addTime":"2022-10-12 22:39:46","updateTime":"2022-10-12 23:22:05"}
     * @param issueUpdteBo
     * @return java44th.group.bean.admin.vo.BaseRespVo
     * @author 小焦
     * @since 2022/10/12 下午 11:20
     */
    @PostMapping("update")
    public BaseRespVo update(@RequestBody IssueUpdteBo issueUpdteBo){
        MarketIssue issue= null;
        try {
            issue = issueService.update(issueUpdteBo);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.error(402, "参数值不对");
        }
        return BaseRespVo.ok(issue);
    }
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody IssueUpdteBo issueUpdteBo){
        int code= 0;
        try {
            code = issueService.delete(issueUpdteBo);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.error(402, "参数值不对");
        }
        if(code!=1){
            return BaseRespVo.invalidParameter("删除失败");
        }
        return BaseRespVo.ok();
    }
}
